package com.bizvane.esDemo.controllers;


import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;

/**
 * @Auther: xiao.shu
 * @Date: 2019/2/19 11:12
 * @Description:
 * @Copyright (c) 2018 上海商帆信息科技有限公司-版权所有
 */
@RestController
public class BookController {

    @Autowired
    private TransportClient client;

    @RequestMapping("/get")
    public String getIndex() {
        return "index";
    }

    @GetMapping("/get/book/novel")
    public ResponseEntity<?> get(@RequestParam(name = "id", defaultValue = "") String id) {
        if (id.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        GetResponse result = this.client.prepareGet("book", "novel", id).get();
        if (!result.isExists()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(result.getSource(), HttpStatus.OK);
    }

    @PostMapping("/add/book/novel")
    public ResponseEntity<?> add(@RequestParam(name = "title") String title, @RequestParam(name = "author") String author, @RequestParam(name = "word_count") int wordCount, @RequestParam(name = "publish_date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishDate) {
        XContentBuilder xContentBuilder = null;
        try {
            xContentBuilder = XContentFactory.jsonBuilder().startObject().field("title", title).field("author", author).field("word_count", wordCount).field("publish_date", publishDate.getTime()).endObject();
            IndexResponse result = this.client.prepareIndex("book", "novel").setSource(xContentBuilder).get();
            return new ResponseEntity<>(result.getId(), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/update/book/novel")
    public ResponseEntity<?> update(@RequestParam(name = "id", defaultValue = "") String id, @RequestParam(name = "title") String title, @RequestParam(name = "author") String author) {
        UpdateRequest updateRequest = new UpdateRequest("book", "novel", id);
        XContentBuilder xContentBuilder = null;
        try {
            xContentBuilder = XContentFactory.jsonBuilder().startObject();
            if (!StringUtils.isEmpty(title)) {
                xContentBuilder.field("title", title);
            }
            if (!StringUtils.isEmpty(author)) {
                xContentBuilder.field("author", author);
            }
            xContentBuilder.endObject();
            updateRequest.doc(xContentBuilder);
            UpdateResponse result = this.client.update(updateRequest).get();
            return new ResponseEntity<>(result.toString(), HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/delete/book/novel")
    public ResponseEntity<?>delete(@RequestParam(name="id")String id){
        DeleteRequestBuilder result = this.client.prepareDelete("book", "novel", id);
        return new ResponseEntity<>(result.toString(),HttpStatus.OK);
    }


}
